package Model;

import javax.swing.*;
import java.awt.*;

public class Buttons extends JButton {
    private String[] imageURl;
    private final boolean hasImage;

    /**
     * Buttons的无参构造函数
     *
     * <p>用于无图片的按钮</p>
     */
    public Buttons() {
        super();
        this.hasImage = false;
    }

    /**
     * Buttons的含参构造函数
     *
     * <p>用于有图片并且有传递信息的按钮</p>
     *
     * @param imageURl 图片地址
     * @param text 按钮的文本信息
     */
    public Buttons(String[] imageURl, String text) {
        super(text);
        this.hasImage = true;
        this.imageURl = imageURl;
        setCursor(new Cursor(Cursor.HAND_CURSOR));
        repaint();
    }

    /**
     * 对按钮进行绘制
     *
     * @param graphics 绘图类的对象，用于绘图
     */
    @Override
    protected void paintComponent(Graphics graphics) {
        ButtonModel model = getModel();
        Image image;
        if (hasImage) {
            if (model.isPressed()) {    //当按钮被按下
                image = Toolkit.getDefaultToolkit().getImage(Buttons.class.getClassLoader().getResource(imageURl[0]));
                graphics.drawImage(image, 0, 0, null);
                repaint();
            } else if (model.isRollover()) {  //当指针停留在按钮上
                image = Toolkit.getDefaultToolkit().getImage(Buttons.class.getClassLoader().getResource(imageURl[1]));
                graphics.drawImage(image, 0, 0, null);
                repaint();
            } else {  //其他事件
                image = Toolkit.getDefaultToolkit().getImage(Buttons.class.getClassLoader().getResource(imageURl[2]));
                graphics.drawImage(image, 0, 0, null);
                repaint();
            }
        }
    }
}
